'use strict';

const BaseController = require('./base');
const { success, commonErr } = require('../extend/helper');
const {Op} = require('sequelize');

class UserController extends BaseController {

    /**
     * 列表
     */
    async list() {
        const page = parseInt(this.ctx.query.page) ? parseInt(this.ctx.query.page) : 0;
        const size = parseInt(this.ctx.query.size) ? parseInt(this.ctx.query.size) : 15;
        const where = await this.where();
        const { rows, count } = await this.ctx.model.User.findAndCountAll({
            where: where,
            order: [
                ['create_time', 'desc']
            ],
            offset: page * size,
            limit: size
        });
        success({
            beans: {
                rows, count, page, size

            }
        })

    }
    
    async where() {
        const nickname = this.ctx.query.nickname;
        const phone = this.ctx.query.phone;
        const sex = this.ctx.query.sex;
        const vip =  this.ctx.query.vip;
        const identity = this.ctx.query.identity;
        const education = this.ctx.query.education;
        const where = {};
        if (nickname) {
            where.nickname = {
                [Op.like]: `%${nickname}%`
            }
        }
        if (phone) {
            where.phone = phone;
        }
        if (sex) {
            where.sex = sex;
        }
        if (identity) {
            where.identity = identity;
        } 
        if (education) {
            where.education = education;
        }
        if (vip == 'true') {
            where.vip = true;
        } 
        if (vip == 'false') {
            where.vip = false;
        }
        return where;
    }

    /**
     * 详情
     */
    async detail() {
        const id = this.ctx.params.id;
        const user = await this.ctx.model.User.findByPk(id, {
            include: [
                {
                    model: this.ctx.model.UserImage,
                    as: "images",
                    attributes: ['id', 'image']
                },
                {
                    model: this.ctx.model.UserCertificate,
                    as: "certificate",
                },
            ]
        })
        success({bean: user});
    }

    /**
     * 删除
     */
    async delete() {
        const user = await this.getObject();
        user.destroy();
        success();
    }

    /**
     * 身份审核
     */
    async identity() {
        const status = this.ctx.query.status;
        if (!status) {
            commonErr("状态不能为空")
        }
        const user = await this.getObject();
        if (user.identity != 1) {
            commonErr("身份已审核，无需再次操作")
        }
        if (status == 'agree') {
            user.identity = 2;
        }
        if (status == 'refuse') {
            user.identity = 3;
        }
        user.save();
        success();
    }

    /**
     * 学历审核
     */
    async education() {
        const status = this.ctx.query.status;
        if (!status) {
            commonErr("状态不能为空")
        }
        const user = await this.getObject();
        if (user.education != 1) {
            commonErr("身份已审核，无需再次操作")
        }
        if (status == 'agree') {
            user.education = 2;
        }
        if (status == 'refuse') {
            user.education = 3;
        }
        user.save();
        success();
    }

    async getCertificate(user_id) {
        const user = await this.getObject();
        const certificate = await this.ctx.model.UserCertificate.findOne({
            where: {
                user_id: user.id
            }
        })
        return certificate;
    }


    async getObject() {
        const id = this.ctx.params.id;
        const user = await this.ctx.model.User.findByPk(id);
        if (!user) {
            commonErr(`用户不存在`);
        }
        return user;
    }

}

module.exports = UserController;
